﻿Imports System.IO
Imports System.Xml
Imports System.Text
Imports System.Configuration.ConfigurationManager
Imports Slythe.FileSystemProfiler.ProfilerService.Utilities



Namespace Serialisation



    Public Class Serialisation_XML



        Public Shared Sub serialiseAll(ByVal files As List(Of FileInfo), _
                                       ByVal directories As List(Of DirectoryInfo))


            Dim xmlSettings As New XmlWriterSettings

            With xmlSettings
                .Indent = True
                .IndentChars = vbTab
                .Encoding = Encoding.UTF8
                .NewLineOnAttributes = False
            End With


            Dim fileName As String = _
                Path.Combine(AppSettings("Serialisation.SaveDirectory"), _
                             "FileSystemProfiler_" & _
                             DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") & _
                             ".xml")

            Using writer As XmlWriter = XmlWriter.Create(fileName, xmlSettings)


                writer.WriteStartElement("FileSystemProfile")


                writeDirectories(writer, _
                                 directories)

                writer.Flush()


                writeFiles(writer, _
                           files)

                writer.Flush()


                writer.WriteEndElement()


            End Using


        End Sub



        Private Shared Sub writeFiles(ByVal writer As XmlWriter, _
                                      ByVal files As List(Of FileInfo))


            writer.WriteStartElement("files")

            For Each objFile As FileInfo In files

                writer.WriteStartElement("file")
                writer.WriteElementString("name", objFile.Name)
                writer.WriteElementString("fileSize", UnitConverter.ConvertBetweenDigitalStorageUnits(DigitalStorageUnits.BYTE_UNIT, _
                                                                                                      objFile.Length, _
                                                                                                      DigitalStorageUnits.MEGABYTE_UNIT))
                writer.WriteElementString("fileType", objFile.Extension)
                writer.WriteElementString("directory", objFile.Directory.FullName)
                writer.WriteElementString("createdDate", objFile.CreationTimeUtc)

                Dim lastModified As DateTime = DateTime.MinValue
                DateTime.TryParse(objFile.CreationTimeUtc.ToString, lastModified)

                writer.WriteElementString("lastModified", lastModified)


                writer.WriteEndElement()


            Next

            writer.WriteEndElement()


        End Sub



        Private Shared Sub writeDirectories(ByVal writer As XmlWriter, _
                                            ByVal directories As List(Of DirectoryInfo))


            writer.WriteStartElement("directories")

            For Each objDir As DirectoryInfo In directories

                writer.WriteStartElement("directory")

                writer.WriteElementString("name", objDir.FullName)

                writer.WriteEndElement()

            Next

            writer.WriteEndElement()


        End Sub



    End Class



End Namespace